package com.jprocms.module.cms.controller.portal.api;

import com.jpro.framework.common.pojo.CommonResult;
import com.jpro.framework.security.core.LoginUser;
import com.jpro.framework.security.core.util.SecurityFrameworkUtils;
import com.jpro.module.infra.api.file.FileApi;
import com.jpro.module.infra.api.file.dto.ImgCutDto;
import com.jpro.module.infra.api.file.vo.ImgCutVo;
import com.jpro.module.system.api.user.AdminUserApi;
import com.jpro.module.system.api.user.dto.AdminUserRespDTO;
import com.jpro.module.system.api.user.dto.MemberUserRespDTO;
import com.jpro.module.system.api.user.dto.UserAvatarDto;
import com.jpro.module.system.api.user.vo.UserProfileUpdatePasswordReqVO;
import com.jpro.module.system.api.user.vo.UserProfileUpdateReqVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.security.PermitAll;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Map;

import static com.jpro.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.jprocms.module.cms.constant.CmsSysConstants.FRONT_API;
import static com.jprocms.module.cms.enums.ErrorCodeConstants.PORTAL_USER_NOT_LOGIN;

/**
 * 会员登录后 接口
 *
 * @author jprocms
 */
@Slf4j
@Tag(name = "前台 - 会员登录后接口")
@RestController
@RequestMapping({FRONT_API + "/member"})
public class MemberApiController {
    private final AdminUserApi adminUserApi;
    private final FileApi fileApi;

    public MemberApiController(AdminUserApi adminUserApi, FileApi fileApi) {
        this.adminUserApi = adminUserApi;
        this.fileApi = fileApi;
    }

    @PermitAll
    @GetMapping({"/user", "/{sitePath:[\\w-]+}/user"})
    public CommonResult<MemberUserRespDTO> userCenter(@PathVariable(required = false) String sitePath, Map<String, Object> modelMap) {
        final LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        if (loginUser == null) {
            throw exception(PORTAL_USER_NOT_LOGIN);
        }
        return adminUserApi.getMember(loginUser.getId());
    }

    @PermitAll
    @PostMapping("/update")
    @Operation(summary = "修改用户个人信息")
    public CommonResult<Boolean> updateUserProfile(@Valid @RequestBody UserProfileUpdateReqVO reqVO) {
        return adminUserApi.updateUserProfile(reqVO);
    }

    @PermitAll
    @PostMapping("/update-password")
    @Operation(summary = "修改用户个人密码")
    public CommonResult<Boolean> updateUserProfilePassword(@Valid @RequestBody UserProfileUpdatePasswordReqVO reqVO) {
        return adminUserApi.updateUserProfilePassword(reqVO);
    }

    @PermitAll
    @RequestMapping(value = "/update-avatar", method = RequestMethod.POST)
    @Operation(summary = "上传用户个人头像")
    public CommonResult<String> updateUserAvatar(@RequestParam("avatarFile") MultipartFile file) throws Exception {
        return adminUserApi.updateUserAvatar(file);
    }

    @PermitAll
    @PostMapping(value = "/update-avatar-url")
    @Operation(summary = "上传用户个人头像")
    public CommonResult<String> updateUserAvatarUrl(@RequestBody UserAvatarDto avatarDto) {
        return adminUserApi.updateUserAvatarUrl(avatarDto);
    }

    @PermitAll
    @RequestMapping(value = "/image-crop", method = RequestMethod.POST)
    @Operation(summary = "裁剪图片")
    public CommonResult<ImgCutVo> imageCrop(@RequestBody ImgCutDto params) throws Exception {
        return fileApi.imageCrop(params);
    }


    @Operation(summary = "用户名是否未使用")
    @GetMapping("/usernameNotExist")
    public Boolean usernameNotExist(@Parameter(description = "用户名") @NotBlank String username) {
        final CommonResult<AdminUserRespDTO> userRespDTOCommonResult = adminUserApi.selectByUsername(username);
        if (userRespDTOCommonResult.isError()) {
            return false;
        }
        return userRespDTOCommonResult.getData() == null;
    }

    @Operation(summary = "手机号码是否未使用")
    @GetMapping("/mobileNotExist")
    public Boolean mobileNotExist(@Parameter(description = "手机号码") @NotBlank String mobile) {
        final CommonResult<AdminUserRespDTO> userRespDTOCommonResult = adminUserApi.selectByMobile(mobile);
        if (userRespDTOCommonResult.isError()) {
            return false;
        }
        return userRespDTOCommonResult.getData() == null;
    }

    @Operation(summary = "邮箱地址是否未使用")
    @GetMapping("/emailNotExist")
    public boolean emailNotExist(@Parameter(description = "邮箱地址") @NotBlank String email) {
        final CommonResult<AdminUserRespDTO> userRespDTOCommonResult = adminUserApi.selectByEmail(email);
        if (userRespDTOCommonResult.isError()) {
            return false;
        }
        return userRespDTOCommonResult.getData() == null;
    }
}
